﻿using CPPEI.Coolzon.UI.XProperty;
using CPPEI.Coolzon.UI.XProperty.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.UI.PetroleumDataPartLib.XProperty.Converters
{
    /// <summary>
    /// 业务服务地址转换
    /// </summary>
    public class XFeatureLayConverter : IPropertyValueConverter
    {
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="context"></param>
        /// <param name="valueNode"></param>
        /// <returns></returns>
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            List<FeatureLayer> layers = new List<FeatureLayer>();
            foreach (Metadata.ValueNode vNode in valueNode.ValueNodes)
            {
                layers.Add(new FeatureLayer()
                {
                    LayerId = vNode.Attributes.ContainsKey("LayerId") ? vNode.Attributes["LayerId"] : "",
                    Url = vNode.Attributes["Url"]
                });

            }
            return layers;
        }

        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            List<FeatureLayer> featureList = value as List<FeatureLayer>;
            if (featureList == null || featureList.Count == 0)
            {
                return null;
            }

            Metadata.ValueNode rootNode = new Metadata.ValueNode("FeatureLayerList");
            rootNode.Attributes = new Dictionary<string, string>();
            rootNode.ValueNodes = new List<Metadata.ValueNode>();
            
            foreach(var item in featureList)
            {
                Metadata.ValueNode valueNode = new Metadata.ValueNode("FeatureLayer");
                valueNode.Attributes = new Dictionary<string, string>();
                valueNode.Attributes.Add("LayerId", System.Convert.ToString(item.LayerId));
                valueNode.Attributes.Add("Url", System.Convert.ToString(item.Url));
                rootNode.ValueNodes.Add(valueNode);
            }

            return rootNode;
        }
    }
}
